package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReRenewalRecord;
import com.iretailer.report.mapper.RenewalRecordMapper;
import com.iretailer.report.model.RenewalRecord;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class RenewalRecordService extends ServiceImpl<RenewalRecordMapper, RenewalRecord> {

    private final RenewalRecordMapper renewalRecordMapper;

    public RenewalRecordService(RenewalRecordMapper renewalRecordMapper) {
        this.renewalRecordMapper = renewalRecordMapper;
    }

    public List<Map<String, Object>> getAutomaticRenewalMalls() {
        return this.renewalRecordMapper.getAutomaticRenewalMalls();
    }


    public List<Map<String, Object>> getRenewalCompletedMalls() {
        return this.renewalRecordMapper.getRenewalCompletedMalls();
    }

    public PageResult<Map<String, Object>> renewalRecordPage(ReRenewalRecord reRenewalRecord) {
        QueryOption queryOption = reRenewalRecord.getQuery();
        QueryWrapper<?> queryWrapper = new QueryWrapper<>();
        String mallName = reRenewalRecord.getMallName();
        queryWrapper.eq("1", 1);
        if (StringUtils.isNotBlank(mallName)) {
            queryWrapper.like("MallName", mallName);
        }
        Page<RenewalRecord> page = new Page<>();
        queryOption.judge(page, queryWrapper);
        IPage<Map<String, Object>> resultPage = renewalRecordMapper.selectRenewalRecordPage(page, queryWrapper);
        return new PageResult<>(resultPage.getTotal(), resultPage.getRecords());
    }
}
